package com.pie4cloud.pie.job.biz.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pie4cloud.pie.common.core.util.R;
import com.pie4cloud.pie.common.log.annotation.SysLog;
import com.pie4cloud.pie.common.security.annotation.Inner;
import com.pie4cloud.pie.job.api.entity.PlaybillEntity;
import com.pie4cloud.pie.job.biz.service.PlayBillService;
import com.pie4cloud.pie.job.biz.thread.ScheduleThread;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/playbill" )
public class PlayBillController {
	private final PlayBillService playBillService;

	private final ScheduleThread scheduleThread;

	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page" )
	public R getTDataJobInfoPage(Page page, PlaybillEntity playbillEntity) {
		return R.ok(playBillService.page(page,playbillEntity));
	}

	@ApiOperation(value = "查询所有", notes = "查询所有")
	@GetMapping("/list" )
	public R getAllTDataJobInfo() {
		return R.ok(playBillService.findAll());
	}

	/**
	 * 通过id查询
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}" )
	public R getById(@PathVariable("id" ) Long id) {
		return R.ok(playBillService.getById(id));
	}

	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/remote/{id}" )
	@Inner
	public R<PlaybillEntity> getRemoteById(@PathVariable("id" ) Long id) {
		return R.ok(playBillService.getById(id));
	}

	/**
	 * 新增
	 * @param  playbillEntity
	 * @return R
	 */
	@ApiOperation(value = "新增", notes = "新增")
	@SysLog("新增" )
	@PostMapping
	public R save(@RequestBody PlaybillEntity playbillEntity) {
		return R.ok(playBillService.save(playbillEntity));
	}

	/**
	 * 修改
	 * @param playbillEntity
	 * @return R
	 */
	@ApiOperation(value = "修改", notes = "修改")
	@SysLog("修改" )
	@PutMapping
	public R updateById(@RequestBody PlaybillEntity playbillEntity) {
		return R.ok(playBillService.update(playbillEntity));
	}

	/**
	 * 通过id删除
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除", notes = "通过id删除")
	@SysLog("通过id删除" )
	@DeleteMapping("/{id}" )
	public R removeById(@PathVariable Long id) {
		return R.ok(playBillService.removeById(id));
	}


	@ApiOperation(value = "立即执行", notes = "立即执行")
	@SysLog("立即执行任务" )
	@GetMapping("/execute/{id}" )
	public R execute(@PathVariable Long id) {
		PlaybillEntity playbillEntity=playBillService.getById(id);
		scheduleThread.handT(playbillEntity);
		return R.ok();
	}
	@ApiOperation(value = "资料下拉框", notes = "资料下拉框")
	@GetMapping("/findAll" )
	public R<List<PlaybillEntity>> findAll(){
        return R.ok(playBillService.findAll());
	}

	@GetMapping("/findByDdataId/{ddataId}" )
	@Inner
	public R<PlaybillEntity> findByDdataId(@PathVariable("ddataId" ) String ddataId) {
		return R.ok(playBillService.findByDdataId(ddataId));
	}
}
